// Package service @program: chat
//@author: edte
//@create: 2021-12-28 16:26
package service

import (
	"chat/config"
	"chat/database"
	log2 "chat/log"
	"context"
	"fmt"
	"log"
	"net/http"
	"strconv"
)

type ChatSystem struct {
	chat.UnimplementedProdChatSystemServiceServer
}

func (cs ChatSystem) GetUserCreateChatRoomForm(ctx context.Context, in *chat.CreateChatRoomRequest) (response *chat.CreateChatRoomResponse, err error) {
	err = database.CreateChatRoom(ctx, int(in.RoomId), int(in.UserId), in.IsPrivate)
	response = &chat.CreateChatRoomResponse{}
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserCreateChatRoomForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserCreateChatRoomForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Debug", "Succeeded in creating a room")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	response.Status = http.StatusOK
	return response, err
}
func (cs ChatSystem) GetUserDisBandChatRoomForm(ctx context.Context, in *chat.DisbandChatRoomsRequest) (response *chat.DisbandChatRoomResponse, err error) {
	err = database.QuitChatRoom(ctx, int(in.RoomId), int(in.UserId))
	response = &chat.DisbandChatRoomResponse{}
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserDisBandChatRoomForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}

	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserDisBandChatRoomForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Debug", "Exit room successfully")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}

	response.Status = http.StatusOK
	return response, err

}
func (cs ChatSystem) GetUserJoinChatRoomForm(ctx context.Context, in *chat.JoinChatRoomRequest) (response *chat.JoinChatRoomResponse, err error) {
	err = database.JoinChatRoom(ctx, int(in.RoomId), int(in.UserId))
	response = &chat.JoinChatRoomResponse{}
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserJoinChatRoomForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return nil, err
	}

	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserJoinChatRoomForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Debug", "Succeeded in joining the room")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}

	response.Status = http.StatusOK
	return response, err

}
func (cs ChatSystem) GetUserChatRoomMasterAddUserForm(ctx context.Context, in *chat.ChatRoomMasterAddUserRequest) (response *chat.ChatRoomMasterAddUserResponse, err error) {
	err = database.MasterAddUser(ctx, int(in.RoomId), int(in.UserId), int(in.MasterId))
	response = &chat.ChatRoomMasterAddUserResponse{}
	if err != nil {

		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserChatRoomMasterAddUserForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		return response, err
	}

	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserChatRoomMasterAddUserForm", strconv.Itoa(int(in.RoomId)), "Debug", "The owner succeeded in adding a user")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}

	response.Status = http.StatusOK
	return response, err
}
func (cs ChatSystem) GetUserQueryChatRoomForm(ctx context.Context, in *chat.QueryChatRoomRequest) (response *chat.QueryChatRoomResponse, err error) {
	message2, err := database.QueryChatMessage2(ctx, int(in.RoomId))
	response = &chat.QueryChatRoomResponse{}
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserQueryChatRoomForm", strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		response.Messages = nil
		return response, err
	}

	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserQueryChatRoomForm", strconv.Itoa(int(in.RoomId)), "Debug", "The administrator succeeded in querying the chat room information")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}

	response.Messages = message2
	response.Status = http.StatusOK
	return response, err
}

func (cs ChatSystem) GetUserChatRoomMessageForm(ctx context.Context, in *chat.QueryUserChatRoomRequest) (response *chat.QueryUserChatRoomResponse, err error) {
	message, err := database.QueryChatMessage(ctx, int(in.RoomId), int(in.UserId))
	response = &chat.QueryUserChatRoomResponse{}
	if err != nil {

		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserChatRoomMessageForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		response.Messages = nil

		return response, err
	}

	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserChatRoomMessageForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Debug", "The user succeeded in querying the message. Procedure")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}

	response.Messages = message
	response.Status = http.StatusOK
	return response, err
}
func (cs ChatSystem) GetUserSentMessageForm(ctx context.Context, in *chat.SentMessageRequest) (response *chat.SentMessageResponse, err error) {
	message, err := database.SendMessage(ctx, int(in.RoomId), int(in.UserId), in.Message, in.Nickname)
	response = &chat.SentMessageResponse{}
	if err != nil {

		writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserSentMessageForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		response.Message = ""
		return response, err
	}

	writeLog, err2 := log2.WriteLog(ctx, "chat", "GetUserSentMessageForm", "userid:"+strconv.Itoa(int(in.UserId))+" roomid:"+strconv.Itoa(int(in.RoomId)), "Debug", "Message sending succeeded,"+message)
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}

	response.Message = message
	response.Status = http.StatusOK
	return response, err
}

func (cs ChatSystem) ChatInTheGameBureau(ctx context.Context, in *chat.ChatInTheGameBureauRequest) (response *chat.ChatInTheGameBureauResponse, err error) {
	response = &chat.ChatInTheGameBureauResponse{}
	bureau, err := database.ChatInTheGameBureau(ctx, int(in.GameRoomId), int(in.PlayerAId), int(in.PlayerBId))
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAid:"+strconv.Itoa(int(in.PlayerAId))+"userBid:"+strconv.Itoa(int(in.PlayerBId))+" roomid:"+strconv.Itoa(int(in.GameRoomId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		response.ChatRoomId = 0
		return response, err
	}

	log.Println(bureau)
	response.Status = http.StatusOK
	response.ChatRoomId = int32(bureau)
	log.Println(response.ChatRoomId)
	log.Println(int64(response.ChatRoomId))
	return response, err
}
